package com.cqyc.shop.controller;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqyc.shop.comm.commException;
import com.cqyc.shop.domain.Shop;
import com.cqyc.shop.services.ShopServices;


@WebServlet(urlPatterns="/adminadd.do")
public class AdminaddServlet extends HttpServlet {
	private static final long serialVersionUID = 5912725949740503148L;
	private Logger logger = LoggerFactory.getLogger(getClass());  
	private ShopServices shopServices = new ShopServices();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("详情页面的显示");
		try {
			//1:从前台获取页面
			logger.debug("从前台获取参数");
			String id =	request.getParameter("id");
			String num = request.getParameter("num");
			logger.debug("{}",num);
			if(num==null) {
				num="1";
			}
			BigDecimal num2 = new BigDecimal(num); 
			request.setAttribute("num2", num2);
			Shop shop = new Shop();
			logger.debug("{}",num2);
			//2:数据转换
			Integer idd = Integer.valueOf(id);
			if(id!=null) {
				shop.setId(idd);
			}else {
				throw new commException("获取ID为空，取不到商品信息");
			}
			
			
			//3:数据校验
			//4:业务处理
		
			Shop shop2 = shopServices.findId(idd);
			
			//获取购物车的对象
		
			logger.debug("{}",shop2.getPrice());
			BigDecimal  price = num2.multiply(shop2.getPrice());
			request.setAttribute("shop2", shop2);
			HttpSession session = request.getSession();
			session.setAttribute("shop2", shop2);
			request.setAttribute("price", price);
			logger.debug("{}",price);
			//5:跳转
			request.getRequestDispatcher("/Adminadd.jsp").forward(request, response);
		} catch (Exception e) {
			throw new commException("商品信息查找出现异常");
		}
		
	}
	
}
